/*
 * JBeagle - a Java toolkit for genetic algorithms.
 * 
 * Copyright (c) 2010 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package jbeagle.config;

import org.apache.log4j.Logger;
import org.jdom.*;
import org.jdom.input.*;
import java.io.IOException;
import java.util.List;

public class Parser {

	static Logger log = Logger.getLogger(Parser.class);
	
	public static Configuration parse(String filename) {
		SAXBuilder builder = new SAXBuilder();
		
		Document doc = null;
		try {
			doc = builder.build(filename);
		} catch (IOException e) {
			log.error("Error opening file " + filename + "; " + e.getMessage());
			return null;
		} catch (JDOMException e) {
			log.error("Error parsing file " + filename + "; " + e.getMessage());
			return null;
		}
		
		return createConfig( doc.getRootElement() );
	}
	
	private static Configuration createConfig(Element e) {
		Configuration config = new Configuration(e.getAttributeValue("name"));
		
		List<Element> values = e.getChildren("value");		
		for (Element v : values) {
			config.addValue(v.getAttributeValue("name"), v.getText());
		}
		
		Object obj;
		List<Element> objects = e.getChildren("object");
		for (Element o : objects) {
			log.debug("Creating object " + o.getAttributeValue("name"));
			obj = createObject(o);
			if (obj == null) {
				log.warn("Object " + o.getAttributeValue("name") + " is null.");
				return null;
			}
			config.addObject(o.getAttributeValue("name"), obj);
		}
		
		List<Element> configs = e.getChildren("configuration");
		Configuration c;
		for (Element el : configs) {
			c = createConfig(el);
			if (c == null)
				return null;
			config.addConfig(c.getName(), c);
		}
		
		return config;		
	}
	
	private static Object createObject( Element e) {
		
		String name = e.getAttributeValue("classname");
	
		List<Element> argList = e.getChildren(); //either primitive args or other objects
		
		String[] 	argTypes = new String[argList.size()];
		Object[]	args = new Object[argList.size()];
		
		int i = 0;
		for (Element arg : argList) {
			if ( arg.getName().equals("object") ) {
				argTypes[i] = arg.getAttributeValue("classname");
				args[i++] = createObject(arg);
			} else { //element is primitive arg
				argTypes[i] = arg.getAttributeValue("type");
				args[i++] = arg.getValue();
			}
		}
		
		return ObjectFactory.create(name, argTypes, args);
	}
}
